package com.wangsd.web.face.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.CommonStatic;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.Device;
import com.wangsd.web.face.entity.Store;
import com.wangsd.web.face.entity.Store;
import com.wangsd.web.face.service.AdvertisementService;
import com.wangsd.web.face.service.AdvertisementStoreRelationService;
import com.wangsd.web.face.service.DeviceService;
import com.wangsd.web.face.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.NO_ASSUPER_AUTH;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-10
 */
@Api(value = "store", tags = "门店管理")
@RestController
@RequestMapping("/face/store")
public class StoreController extends MyController {

    @Autowired
    AdvertisementStoreRelationService advertisementStoreRelationService;

    @Autowired
    StoreService storeService;
    @Autowired
    DeviceService deviceService;

    @Autowired
    AdvertisementService advertisementService;

    @ApiOperation(value = "查询门店列表", notes = "查询门店列表", httpMethod = "POST")
    @RequestMapping(value = "/selectList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<Store>> selectList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                           @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                           @RequestBody @ApiParam(name = "params", value = "传入json格式") Store params) {
        SysUser loginUser = getUserInfo();
        IPage<Store> resultIPage = storeService.selectList(pageNum, pageSize, params, loginUser.getCode());
        return Result.success(resultIPage);
    }


    @ApiOperation(value = "新增或修改门店", notes = "新增或修改门店", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public Result addOrUpdate(Store params) {
        storeService.addOrUpdate(params);
        return Result.success();
    }

    @ApiOperation(value = "查询所有门店", notes = "查询所有门店", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<Map> list = storeService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }

    @ApiOperation(value = "删除门店信息", notes = "删除门店信息", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@ApiParam(value = "用户id", required = true) @PathVariable("id") Long id) {
        // 清空门店设备信息
        deviceService.removeDeviceStoreIdByStoreId(id);
        //删除门店
        storeService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "通过id查询门店", notes = "通过id查询门店", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectById/{id}")
    public Result<Store> selectById(@ApiParam(value = "服务商id", required = true) @PathVariable Long id) {
        Store result = storeService.getById(id);
        return Result.success(result);
    }

    @ApiOperation(value = "通过门店id查询已绑定设备列表", notes = "通过门店id查询已绑定设备列表", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectDeviceList/{id}")
    public Result selectDeviceList(@ApiParam(value = "门店", required = true) @PathVariable Long id) {
        List<Device> list = deviceService.selectByStoreId(id);
        return Result.success(list);
    }

    @ApiOperation(value = "删除门店设备信息", notes = "删除门店设备信息", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "deleteStroeDevice", method = RequestMethod.POST)
    public Result delMerchantReceiver(@ApiParam(value = "设备id", required = true) @RequestParam Long deviceId) {
        deviceService.deleteDeviceStoreId(deviceId);
        return Result.success();
    }


    @ApiOperation(value = "新增门店设备", notes = "新增门店设备", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "addStoreDevice", method = RequestMethod.GET)
    public Result addStoreDevice(@ApiParam(value = "门店id", required = true) @RequestParam Long storeId,
                                 @ApiParam(value = "设备id", required = true) @RequestParam Long deviceId) {
        SysUser userInfo = getUserInfo();
        if (CommonStatic.IS_SUPER_MANAGER == userInfo.getType()) {
            return Result.message(NO_ASSUPER_AUTH);
        }

        Device device = deviceService.getById(deviceId);
        if (device != null && device.getStoreId() != null) {
            Store store = storeService.getById(device.getStoreId());
            return Result.fail("请先取消当前选择设备与[" + store.getName() + "]门店的绑定");
        }
        deviceService.addDeviceStoreId(storeId,deviceId);
        return Result.success();
    }

    @ApiOperation(value = "查询层级关系树形结构", notes = "查询层级关系树形结构", httpMethod = "GET")
    @RequestMapping(value = "/selectTree", method = RequestMethod.GET)
    public Result<IPage<Store>> selectTree(@ApiParam(value = "限制那些层级不能点击", required = true) @RequestParam String ctrlType,
                                           @ApiParam(value = "限制那些层级不能点击", required = true) @RequestParam String displayType

    ) {
        SysUser loginUser = getUserInfo();
        JSONArray result = storeService.selectTree(ctrlType, displayType, loginUser.getType(), loginUser.getCode());
        return Result.success(result);
    }

    @ApiOperation(value = "查询门店广告列表", notes = "查询门店广告列表", httpMethod = "POST")
    @RequestMapping(value = "/selectAdvertisementList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<Store>> selectAdvertisementList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                                        @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                                        @RequestBody @ApiParam(name = "params", value = "传入json格式") Store params) {
        SysUser loginUser = getUserInfo();
        IPage<Store> resultIPage = storeService.selectAdvertisementList(pageNum, pageSize, params, loginUser.getCode());
        return Result.success(resultIPage);
    }

    @ApiOperation(value = "修改门店广告", notes = "修改门店广告", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "updateStoreAdvertisement", method = RequestMethod.POST)
    public Result updateStoreAdvertisement(@RequestBody @ApiParam(value = "门店json", required = true) Store store) {
        if (store == null) {
            return Result.fail("参数不能为空！");
        }
        if (store.getId() == null) {
            return Result.fail("门店id不能为空");
        }

        SysUser userInfo = getUserInfo();
        String code = null;
        if(userInfo.getType()!=99){
            code = userInfo.getCode();
        }else{
            return Result.fail("超级管理员不能操作！");
        }
        List<Long> advertisementIds = store.getAdvertisementIds();
        if (advertisementIds != null && advertisementIds.size() > 0) {
            /*查询当前服务商下的默认广告位*/
            Integer count = advertisementService.selectAdvertisementDefault(code);
            if (advertisementIds != null && (advertisementIds.size() > 8 - count)) {
                return Result.fail("该门店广告位不能超过" + (8 - count) + "个！");
            }
        }
        storeService.updateStoreAdvertisement(store);
        return Result.success();
    }
}

